package com.example.pagingdemo;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.pagingdemo.databinding.ActivityPagingBinding;
import com.example.pagingdemo.model.Movie;
import com.example.pagingdemo.paging.movie.MoviePageListAdapter;
import com.example.pagingdemo.paging.movie.MovieViewModel;

/**
 * @author: tuyrt7 on 2020/9/9.
 */
public class PagePositionActivity extends AppCompatActivity {

    private ActivityPagingBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_paging);

        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setHasFixedSize(true);
        MoviePageListAdapter moviePageListAdapter = new MoviePageListAdapter(this);

        MovieViewModel viewModel = new ViewModelProvider(this).get(MovieViewModel.class);
        viewModel.getPagedData().observe(this, new Observer<PagedList<Movie>>() {
            @Override
            public void onChanged(PagedList<Movie> movies) {
                moviePageListAdapter.submitList(movies);
            }
        });

        mBinding.recyclerView.setAdapter(moviePageListAdapter);
    }
}
